package com.testing.end_recyc_up_down;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout demo_swiperefreshlayout;
    private RecyclerView demo_recycler;
    private LinearLayoutManager linearLayoutManager;
    private RefreshRecyclerAdapter adapter;
    private int lastVisibleItem;
    private int pageNo = 1;
    private ArrayList<Bean.DataBean> arr = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //okhttp_请求数据
        getData(pageNo);

        //找控件
        demo_swiperefreshlayout = (SwipeRefreshLayout) this.findViewById(R.id.demo_swiperefreshlayout);
        demo_recycler = (RecyclerView) this.findViewById(R.id.demo_recycler);

        //布局设置
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        demo_recycler.setLayoutManager(linearLayoutManager);

        //上啦刷新
        demo_swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                arr.clear();
                getData(++pageNo);

                Toast.makeText(MainActivity.this, "刷新", Toast.LENGTH_SHORT).show();

                demo_swiperefreshlayout.setRefreshing(false);//停止刷新
            }
        });

        //下拉加载
        demo_recycler.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //判断RecyclerView停止滚动且停在最后一个条目,就加载新内容
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {

                    getData(++pageNo);
                    Toast.makeText(MainActivity.this, "加载", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //获取最后一个可见条目的索引
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
    }


    //okhttp_请求数据
    public void getData(int num) {
        //接口
        String u = "http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=10&gender=2&ts=1871746850&page=" + num;
        //创建okHttpClient对象
        OkHttpClient okc = new OkHttpClient();
        //创建一个Request
        Request req = new Request.Builder()
                .url(u).build();
        //new call
        Call call = okc.newCall(req);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                //获取结果
                final String json = response.body().string();
                //主线程解析数据,并适配
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Gson g = new Gson();
                        Bean b = g.fromJson(json, Bean.class);

                        List<Bean.DataBean> data = b.data;

                        arr.addAll(data);
                        //适配器对象为空则创建,不为空则刷新
                        if (adapter == null) {
                            adapter = new RefreshRecyclerAdapter(MainActivity.this, arr);
                            demo_recycler.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        });
    }
}
